//This servlet handles the deletion of a broadcaster belonging to  a particular nym.It will be called from the deletebroadcaster.html page 
//Author:Shreyas kamath
import java.sql.*;
import java.util.Date;
import java.io.*;
import java.util.*;
import java.util.HashMap;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;

public class DeleteBroadCasterServlet extends HttpServlet{

	//Initialize the servlet	
	public void init(ServletConfig config)
	{
	  System.out.println("FriendsServlet: init()");
	}
	
	//This is the service method of the servlet which takes the parameters and then calls the 'DeleteBroadCasters' function to perform the  actual deletion from the database.
	public void doPost(HttpServletRequest request,HttpServletResponse response)    
		throws ServletException, IOException 
	{
		
		// set the response type
		response.setContentType("text/xml; charset=UTF-8"); 
		response.setHeader("Cache-Control","no-cache");
		String strNym=request.getParameter("nym");
		//String strNym="Home";
		String strBroadcasters=request.getParameter("broadcasters");
		//String strBroadcasters="FDIC,BBB";
		int success=this.DeleteBroadCasters(strNym,strBroadcasters);
		// obtain the writer to write the response
		PrintWriter out = response.getWriter();
		//Sends back the xml response.Success indicates if the deletion occurred successfully 
		out.println("<?xml version=\"1.0\"?>");  
		out.println("<Success>");
		out.println("<valid>");
		out.println(success);
		out.println("</valid>");
		out.println("</Success>");
	}
		
		
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// Connect()
	// Connects to the database
	// return value of type Connection
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static Connection Connect()
	{
		Connection conn = null;
		
		try
		{
			//Connect to the database
			String userName = "nettrust";
			String password = "nettrust06162006";
			String url= "jdbc:mysql://localhost/NetTrust";
			Class.forName ("com.mysql.jdbc.Driver").newInstance ();
			conn = DriverManager.getConnection (url, userName, password);
			System.out.println ("Database connection established");
		}
		catch( Exception e )
		{
			//Error in connection
			System.out.println("Connection failed");
		};  
		return( conn );
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * *h * * */
	// end of function Connect()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// DeleteBroadCasters()
	// Input parameters are the nymname and the strbroadcasters
	// returns 1 if deletion was done succesfully else returns a 0.
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static int DeleteBroadCasters(String strNymName,String strBroadCasters)
	{
		ResultSet rsBroadCasters;
		Connection conn=Connect();
		int iBroadCasterID=0;
		String strNymID=GetNymID(strNymName);
		//get the names of the broadcasters to be deleted
		StringTokenizer strTokBroadCasterName=new StringTokenizer(strBroadCasters,",");
		try
		{
			Statement stDeleteBroadCasterName=conn.createStatement();
			Statement stBroadCasterID=conn.createStatement();
			//while there still are broadcasters to be deleted
			while(strTokBroadCasterName.hasMoreTokens())
			{
				//obtain the id of the broadcaster
				rsBroadCasters=stBroadCasterID.executeQuery("select BroadCasterID from broadcasters where BroadCasterName= '"+strTokBroadCasterName.nextToken()+"'"); 
				while(rsBroadCasters.next())
				{
					iBroadCasterID=rsBroadCasters.getInt("BroadCasterID");
				}
				//Delete the broadcaster so that the nym no longer subscribes to the broadcaster
				stDeleteBroadCasterName.executeUpdate("delete from broadcasternymmap where BroadCasterID='"+iBroadCasterID+"' and NymID='"+strNymID+"'"); 
			}
			return 1;
		}
		catch(Exception e)
		{
			System.out.println(" The following Exception occured"+e);
			return 0;
		}
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// end of function DeleteBroadCasters()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// GetNymID()
	// Gets the 128 bit NymID corresponding to a nym using the nym name as the parameter
	// returns 128 bit NymID
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
		public static String GetNymID(String strNym)
	{
		Connection conn =Connect();
		String strNymid=null;
		ResultSet rsNymID;
		try
		{
			Statement getNymID= conn.createStatement();
		      //Get the nymid corresponding to the nym name from the database 
			rsNymID=getNymID.executeQuery("select nymid from nymnamemap where nymname='" + strNym + "'");
			while( rsNymID.next() )
			{
				strNymid=rsNymID.getString("nymid");
			}
		}
		catch( Exception e )
		{
			System.out.println ( "Database Exception is " + e );
		};  
		return(strNymid);
	}	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// end of function GetNymID()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */		
	
	
	public void destroy() {
		System.out.println("DeleteBroadCasterServlet: destroy()");
	}
}